package com.hanson.api;

import com.alibaba.fastjson.JSONObject;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.bean.WeiXinConfig;
import com.hanson.common.util.BusinessUtil;
import com.hanson.entity.ApplyCashOutBean;
import com.hanson.entity.ApplyInfoBean;
import com.hanson.entity.UserBean;
import com.hanson.service.ApplyCashOutService;
import com.hanson.service.ApplyInfoService;
import com.hanson.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Api(tags = "申请管理")
@RestController
@RequestMapping("/api/apply")
public class ApplyApiController {
    @Autowired
    private ApplyInfoService applyInfoService;
    @Autowired
    private ApplyCashOutService applyCashOutService;
    @Autowired
    private UserService userService;
    @Autowired
    private WeiXinConfig weiXinConfig;

    @ApiOperation(value = "掌柜、合伙人、vip申请", notes = "type（vip申请：1 合伙人申请：2 掌柜申请：3）")
    @RequestMapping(value = "/addInfo", method = RequestMethod.POST)
    public ResultMessage add(@RequestBody ApplyInfoBean applyInfoBean, HttpServletRequest request) throws Exception {
        String openId = BusinessUtil.getHeaderOpenId(request);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setData(applyInfoService.addApplyInfo(openId, applyInfoBean));
        resultMessage.setMsg("提交申请成功！");
        return resultMessage;
    }

    @ApiOperation(value = "撤销申请", notes = "type（vip申请：1 合伙人申请：2 掌柜申请：3）")
    @RequestMapping(value = "/resetApply/{type}", method = RequestMethod.POST)
    public ResultMessage resetApply(@PathVariable Integer type, HttpServletRequest request) throws Exception {
        String openId = BusinessUtil.getHeaderOpenId(request);
        applyInfoService.deleteByOpenIdAndType(openId, type);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("操作成功！");
        return resultMessage;
    }

    @ApiOperation(value = "获取申请结果", notes = "type（vip申请：1 合伙人申请：2 掌柜申请：3）")
    @RequestMapping(value = "/applyResult/{type}", method = RequestMethod.GET)
    public ResultMessage applyResult(@PathVariable Integer type, HttpServletRequest request) throws Exception {
        String openId = BusinessUtil.getHeaderOpenId(request);
        List<ApplyInfoBean> list = applyInfoService.selectByOpenIdAndType(openId, type);
        if(CollectionUtils.isEmpty(list) && type == 1){
            //vip申请 再看看有没有合伙人申请记录
            list = applyInfoService.selectByOpenIdAndType(openId, 2);
        }
        JSONObject jsonObject = new JSONObject();
        if (CollectionUtils.isNotEmpty(list)) {
            int status = list.get(0).getStatus();
            jsonObject.put("status", status);
            jsonObject.put("type", type);
            if (status == 0) {
                jsonObject.put("msg", "提交审核，申请中……");
            } else if (status == 1) {
                if (type == 1) {
                    jsonObject.put("msg", "申请通过，您已是VIP用户……");
                } else if (type == 2) {
                    jsonObject.put("msg", "申请通过，您已是平台合伙人……");
                } else if (type == 3) {
                    UserBean userBean = userService.selectByOpenId(openId);
                    StringBuilder sb = new StringBuilder();
                    sb.append("申请通过，您已是平台掌柜……").append("<br>");
                    if (null != userBean) {
                        String url = String.format("<a href='%s'>%s</a>", weiXinConfig.getDomian() + "admin", weiXinConfig.getDomian() + "admin");
                        sb.append("后台地址：").append(url).append("<br>");
                        sb.append("分配账号：").append(userBean.getAccountName()).append(" 分配密码：").append(userBean.getPassword());
                    }
                    jsonObject.put("msg", sb.toString());
                }
            } else if (status == 2) {
                jsonObject.put("msg", "申请失败，原因：" + list.get(0).getRemark());
            } else if (status == -1) {
                jsonObject.put("msg", "未支付或未购买商品");
            }
        }
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("操作成功！");
        resultMessage.setData(jsonObject);
        return resultMessage;
    }

    @ApiOperation(value = "提现申请")
    @RequestMapping(value = "/addCashOut", method = RequestMethod.POST)
    public ResultMessage addCashOut(@RequestBody ApplyCashOutBean applyCashOutBean, HttpServletRequest request) throws Exception {
        String openId = BusinessUtil.getHeaderOpenId(request);
        ResultMessage resultMessage = new ResultMessage();
        applyCashOutService.add(applyCashOutBean, openId);
        resultMessage.setMsg("提交申请成功！");
        return resultMessage;
    }

}
